package org.dbp.registros;

import java.beans.PropertyEditorSupport;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.dbp.registros.propertysupport.CadenasDbpRegistro;
import org.dbp.registros.propertysupport.FechaDbpRegistro;
import org.dbp.registros.propertysupport.NumerosDbpRegistro;
/**
 * 
 * Es el almacen donde guarda los registros, normales, como los comparables.
 * 
 * @author David Blanco París
 *
 */
public class AlmacenRegistrosDbp {

	private static AlmacenRegistrosDbp instancia=new AlmacenRegistrosDbp();
	
 	private Map<Class,PropertyEditorSupport> registros;
 	private Map<Class,PropertyEditorSupport> registrosComparable;
 	
	
	public static AlmacenRegistrosDbp obtenerInstancia(){
		return instancia;
	}
	
	public AlmacenRegistrosDbp(){
		this.establecerLosValoresPorDefecto();
	}
	
	public void establecerLosValoresPorDefecto(){

		Locale locale= new Locale("es","ES");
	    NumberFormat numberFormat = NumberFormat.getInstance(locale);
	    numberFormat.setGroupingUsed(true);
	    addComparable(BigDecimal.class,new NumerosDbpRegistro(BigDecimal.class,numberFormat,true));
	    addComparable(BigInteger.class,new NumerosDbpRegistro(BigInteger.class,numberFormat,true));
	    addComparable(Double.class,new NumerosDbpRegistro(Double.class,numberFormat,true));
	    addComparable(Float.class,new NumerosDbpRegistro(Float.class,numberFormat,true));
	    addComparable(Integer.class,new NumerosDbpRegistro(Integer.class,numberFormat,true));
	    addComparable(Long.class,new NumerosDbpRegistro(Long.class,numberFormat,true));
	    addComparable(Short.class,new NumerosDbpRegistro(Short.class,numberFormat,true));
	 	/*
	 	 *  String
	 	 *  Date,Time,Timestamp
	 	 *  */
	    addComparable(String.class, new CadenasDbpRegistro());
	 	
    	String formato ="dd/MM/yyyy";
    	addComparable(Date.class, new FechaDbpRegistro(formato, true));
	}
	
	public <C extends Comparable<C>> void addComparable(Class<C> clase,PropertyEditorSupport propertyEditorSupport){
		if(registrosComparable==null){
			registrosComparable = new HashMap<Class,PropertyEditorSupport>();
		}
		registrosComparable.put(clase, propertyEditorSupport);
		add(clase,propertyEditorSupport);
	}
	public <C> void add(Class<C> clase,PropertyEditorSupport propertyEditorSupport){
		if(registros==null){
			registros = new HashMap<Class,PropertyEditorSupport>();
		}
		registros.put(clase, propertyEditorSupport);
	}
	
	
	public Map<Class,PropertyEditorSupport> obtenerMapeoNormal(){
		return registros;
	}
	
	public Map<Class,PropertyEditorSupport> obtenerMapeoComparables(){
		return registrosComparable;
	}

}
